namespace RushMail.Requests;

public class Pool
{
    private readonly IRushMailClient _client;

    public Pool(IRushMailClient client)
    {
        _client = client;
    }

    /// <summary>
    /// 地址池列表(旧)
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=pool-list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<PoolListResponse>> List() =>
        (await _client.PostAsync(new RushMailRequest("wc-pool", "pool-list"))).ToGeneric<PoolListResponse>();

    /// <summary>
    /// 地址池列表(分页返回)
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=pool-list-page
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<PoolListPageResponse>> ListPage(int categoryId = 0, int page = 1, int limit = 20) =>
        (await _client.PostAsync(new RushMailRequest("wc-pool", "pool-list-page"){
            Body = new Dictionary<string, string>()
            {
                {"category_id", categoryId.ToString()},
                {"page", page.ToString()},
                {"limit", limit.ToString()},
            }
        }))
        .ToGeneric<PoolListPageResponse>();

    /// <summary>
    /// 添加地址池
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=pool-add
    /// </summary>
    /// <returns></returns>
    public virtual async Task<RushMailResponse<int>> Add(PoolAdd body) =>
        (await _client.PostAsync(new RushMailRequest("wc-pool", "pool-add")
        {
            Body = body.ToDictionary()
        }))
        .ToGeneric<int>();
    
    /// <summary>
    ///  修改地址池
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=pool-edit
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Edit(PoolEdit body) =>
        _client.PostAsync(new RushMailRequest("wc-pool", "pool-edit")
        {
            Body = body.ToDictionary()
        });
    
    /// <summary>
    /// 删除地址池
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=pool-delete
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Delete(int poolId) =>
        _client.PostAsync(new RushMailRequest("wc-pool", "pool-delete")
        {
            Body = new Dictionary<string, string>() { {"pool_id", poolId.ToString()} }
        });  
    
    /// <summary>
    /// 导出退订记录
    /// http://edmapi.rushmail.com/api.php?module=wc-pool&action=unsubscribe-list
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> UnsubscribeList(int poolId) =>
        _client.PostAsync(new RushMailRequest("wc-pool", "unsubscribe-list")
        {
            Body = new Dictionary<string, string>() { {"pool_id", poolId.ToString()} }
        });
}

public class PoolListResponse
{
    [JsonProperty("pool_list")]public List<PoolItem> PoolList { get; set; }
}


public class PoolListPageResponse
{
    [JsonProperty("pool_list")]public PoolListPage PoolList { get; set; }
}

public class PoolListPage
{
    public List<PoolItem> List { get; set; }
    public Paginate Page { get; set; }
}

public class Paginate
{
    public int Prev { get; set;}
    public int Next { get; set;}
    public int Current { get; set;}
    public int Pages { get; set;}
    public int Total { get; set;}
}

public class PoolItem
{

    /// <summary>
    /// 地址个数
    /// </summary>
    public int Addrcount { get; set; }
    
    
    /// <summary>
    /// 状态 1 正常 -1 禁用
    /// </summary>
    public int Statust { get; set; }

}

public class PoolAdd
{
    /// <summary>
    /// 名称
    /// </summary>
    public string Name { get; set; }
    
    /// <summary>
    /// 公开名称
    /// </summary>
    [JsonProperty("public_name")]public string PublicName { get; set; }
    
    /// <summary>
    /// 描述说明
    /// </summary>
    public string Description { get; set; }
    
    /// <summary>
    /// 地址池分类 id
    /// </summary>
    [JsonProperty("category_id")]public int CategoryId { get; set; }

    /// <summary>
    /// 是否允许自由订阅 1 允许 -1 不允许
    /// </summary>
    public int Subscription { get; set; } = 1;

    public virtual Dictionary<string, string> ToDictionary() => new Dictionary<string, string>()
    {
        {"name", Name},
        {"public_name", PublicName},
        {"subscription", Subscription.ToString()},
        {"category_id", CategoryId.ToString()},
        {"description", Description},
    };
}

public class PoolEdit : PoolAdd
{
    /// <summary>
    /// 地址池 id
    /// </summary>
    [JsonProperty("pool_id")]public int PoolId { get; set; }

    public override Dictionary<string, string> ToDictionary()
    {
        var result = base.ToDictionary();
        result.Remove("category_id");
        result.Add("pool_id", PoolId.ToString());
        return result;
    }
}

public class UnsubscribeList
{
    public string Address { get; set; }
    
    /// <summary>
    /// 退订时间
    /// </summary>
    public DateTime Create { get; set; }
}
